package com.example.blue.musicplay.http;


import android.net.ParseException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by blue on 2016/9/19.
 */
public class SearchSong {
    private volatile static SearchSong instance = null;
    private static HttpURLConnection connection = null;

    public static SearchSong getInstance() {
        if (instance == null) {
            synchronized (SearchSong.class) {
                instance = new SearchSong();
            }
        }
        return instance;
    }

    public String search(String song_id,String key) {
        String music_url = "http://ting.baidu.com/data/music/links?songIds=" + song_id;
        try {
            URL url = new URL(music_url);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            InputStreamReader reader = new InputStreamReader(
                    connection.getInputStream(), "UTF-8");
            BufferedReader breader = new BufferedReader(reader);
            String line = null;
            StringBuilder builder = new StringBuilder();
            while ((line = breader.readLine()) != null) {
                System.out.println(line);
                builder.append(line);
            }
            return getValueJson(builder.toString(),key);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getValueJson(String jsonString, String key) {
        JSONObject dataJson = null;
        try {
            dataJson = new JSONObject(jsonString);
            JSONObject object = dataJson.getJSONObject("data");
            JSONArray data = object.getJSONArray("songList");
            JSONObject info = data.getJSONObject(0);
            String value = info.getString(key);
            return value;

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void ParseJson(String jsonString) throws JSONException,
            ParseException {
        JSONObject dataJson = new JSONObject(jsonString);
        JSONObject object = dataJson.getJSONObject("data");
        JSONArray data = object.getJSONArray("songList");
        for (int i = 0; i < data.length(); i++) {
            JSONObject info = data.getJSONObject(i);
            String province = info.getString("songId");
            String city = info.getString("artistName");
            String district = info.getString("songName");
            String address = info.getString("songPicRadio");
            System.out.println(province + "\n" + city + "\n" + district + "\n" + address);
            System.out.println("----------------------\n\n");
        }
    }

}
